package com.jxmcloud.business.cut.service.impl.logic;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jxmcloud.business.cut.feign.live.LiveClient;
import com.jxmcloud.business.cut.json.DataConfigActivityTaskLoader;
import com.jxmcloud.business.cut.mapper.database.IUserActivityInfoDBMapper;
import com.jxmcloud.business.cut.mapper.database.IUserWatchLiveTaskDetailDBMapper;
import com.jxmcloud.business.cut.model.database.DBUserActivityInfo;
import com.jxmcloud.business.cut.model.database.DBUserWatchLiveTaskDetail;
import com.jxmcloud.business.cut.service.logic.IUserLiveHandle;

import com.jxmcloud.business.cut.vo.json.ConfigActivityTaskVo;
import com.jxmcloud.business.cut.vo.user.PlayerInfoVo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.Objects;


// 直播逻辑逻辑汇总
@Service
@Slf4j
public class UserLiveHandleImpl implements IUserLiveHandle {
    @Autowired
    private IUserWatchLiveTaskDetailDBMapper userWatchLiveTaskDetailMapper;
    @Autowired
    private IUserActivityInfoDBMapper userActivityInfoDBMapper;
    @Autowired
    private LiveClient liveClient;
    @Autowired
    private DataConfigActivityTaskLoader dataConfigActivityTaskLoader;

    @Override
    public Integer getWatchLiveDays(PlayerInfoVo player,String startDay, String endDay) {
        String resultData = liveClient.getUserStandardDayLimit(player.getBase().getXlId().toString(), startDay, endDay);
        JSONObject jsonObject = JSONUtil.parseObj(resultData);
        if (jsonObject.getInt("code").equals(0)) {
            JSONArray data = jsonObject.getJSONArray("data");
            return data.size();
        }
        return 0;
    }

    @Override
    public DBUserWatchLiveTaskDetail createUserWatchLiveTaskDetail(PlayerInfoVo player) {
        DBUserActivityInfo dbUserActivityInfo = userActivityInfoDBMapper.selectByUidAndActivityIdOnDoing(player.getBase().getUid());
        DBUserWatchLiveTaskDetail dbUserWatchLiveTaskDetail = userWatchLiveTaskDetailMapper.selectOne(
                new LambdaQueryWrapper<DBUserWatchLiveTaskDetail>()
                        .eq(DBUserWatchLiveTaskDetail::getUid, player.getBase().getUid())
                        .eq(DBUserWatchLiveTaskDetail::getActivityId, dbUserActivityInfo.getActivityId())
                        .eq(DBUserWatchLiveTaskDetail::getUserActivityInfoId, dbUserActivityInfo.getId()));
        if (null == dbUserWatchLiveTaskDetail) {
            // 插入数据
            dbUserWatchLiveTaskDetail = new DBUserWatchLiveTaskDetail();
            dbUserWatchLiveTaskDetail.setUid(player.getBase().getUid());
            dbUserWatchLiveTaskDetail.setActivityId(dbUserActivityInfo.getActivityId());
            dbUserWatchLiveTaskDetail.setUserActivityInfoId(dbUserActivityInfo.getId().intValue());
            dbUserWatchLiveTaskDetail.setStatus(0);
            dbUserWatchLiveTaskDetail.setCreateTime(new Date());
            userWatchLiveTaskDetailMapper.insert(dbUserWatchLiveTaskDetail);

        }
        return dbUserWatchLiveTaskDetail;
    }

    @Override
    public boolean judgeIsFinishWatchLive(Integer watchLiveDays, Integer taskCondition) {
        Map<Integer, ConfigActivityTaskVo> configActivityTaskMap = dataConfigActivityTaskLoader.getConfigActivityTaskMap();
        // 遍历任务配置Map的所有值
        boolean flag = true;
        for (ConfigActivityTaskVo taskConfig : configActivityTaskMap.values()) {
            // 判断是否是直播任务（type=1表示直播任务）
            if (Objects.equals(taskConfig.getType(), taskCondition)) {
                int actualDays = watchLiveDays;
                // 判断是否满足条件
                if (actualDays < taskConfig.getCondition()) {
                    flag = false;
                    break;
                }
            }
        }
        return flag;
    }

    @Override
    public Integer getWatchLiveDaysByCondition(DBUserWatchLiveTaskDetail userWatchLiveTaskDetail, Integer taskCondition) {
        Integer testStatus = userWatchLiveTaskDetail.getTestStatus();
        if(testStatus != null && testStatus.equals(1)) {
            Integer testCondition = userWatchLiveTaskDetail.getTestCondition();
            Map<Integer, ConfigActivityTaskVo> configActivityTaskMap = dataConfigActivityTaskLoader.getConfigActivityTaskMap();
            // 遍历任务配置Map的所有值
            for (ConfigActivityTaskVo taskConfig : configActivityTaskMap.values()) {
                // 判断是否是直播任务（type=1表示直播任务）
                if (Objects.equals(taskConfig.getType(), taskCondition) && Objects.equals(taskConfig.getCondition(), testCondition)) {
                    return taskConfig.getCondition();
                }
            }
        }
        return 0;
    }
}
